<template>
  <list-card>
    <el-form ref="queryForm" slot="search" :model="queryParams" inline size="small">
      <el-form-item label="组织名称" prop="name">
        <el-input v-model="queryParams.name" clearable placeholder="请输入组织名称" @keyup.enter.native="handleQuery"/>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" clearable placeholder="组织状态">
          <el-option v-for="item in localDict.dept_status" :key="item.value" :label="item.label" :value="item.value"/>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button icon="el-icon-search" size="mini" type="primary" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <div slot="operate">
      <el-button v-has-permi="['system:dept:add']" icon="el-icon-plus" plain size="mini" type="primary"
                 @click="handleAdd">新增
      </el-button>
      <right-toolbar :show-extend.sync="right.expand" :showSearch.sync="right.search" tree
                     @queryTable="getList"/>
    </div>
    <el-table slot="content" v-loading="loading" :data="dataList" :default-expand-all="right.expand" :lazy="true"
              :load="handleLoad" :tree-props="{children: 'children', hasChildren: 'hasChild'}" row-key="id">
      <el-table-column label="组织编码" prop="code" width="180"/>
      <el-table-column label="组织名称" prop="name" show-overflow-tooltip width="260"/>
      <el-table-column label="组织类型" prop="type">
        <template slot-scope="scope">
          <dict-tag :options="localDict.dept_type" :value="scope.row.type"/>
        </template>
      </el-table-column>
      <el-table-column label="排序" prop="sort" width="200"/>
      <el-table-column label="状态" prop="status" width="100">
        <template slot-scope="scope">
          <dict-tag :options="localDict.dept_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column align="center" class-name="small-padding fixed-width" label="操作">
        <template slot-scope="scope">
          <el-button v-has-permi="['system:dept:edit']" icon="el-icon-edit" size="mini" type="text"
                     @click="handleEdit(scope.row)">修改
          </el-button>
          <el-button v-has-permi="['system:dept:add']" icon="el-icon-plus" size="mini" type="text"
                     @click="handleAdd(scope.row)">新增
          </el-button>
          <el-button v-has-permi="['system:dept:remove']" icon="el-icon-delete" size="mini" type="text"
                     @click="handleDelete(scope.row)">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>
    <dept-model ref="model" @ok="getList"/>
  </list-card>
</template>

<script>
import {child, root} from "@/api/tree";
import {remove} from "@/api/base";
import {getStaticData} from "@/api/common";
import DeptModel from "./components/ModelCard.vue";
import listMixin from "../../../mixins/listMixin";

export default {
  name: "Dept",
  mixins: [listMixin],
  components: {DeptModel,},
  data() {
    return {
      // 遮罩层
      loading: true,
      baseUrl: '/system/dept/',
      right: {
        expand: false,
        search: false
      },
      // 表格树数据
      dataList: [],
      // 查询参数
      queryParams: {
        name: undefined,
        status: undefined
      },
      localDict: {
        dept_type: [],
        dept_status: []
      },

    };
  },
  created() {
    this.getLocalDict();
    this.resetQuery()
  },
  methods: {
    async getLocalDict() {
      await getStaticData('system-dept-type').then(res => {
        this.localDict.dept_type = res.data
      })
      await getStaticData('system-status').then(res => {
        this.localDict.dept_status = res.data
      })
    },
    /** 查询部门列表 */
    getList() {
      this.loading = true;
      root(this.baseUrl, this.queryParams).then(res => {
        this.dataList = res.data;
        this.loading = false;
      });
    },
    // 树形动态加载表单数据：
    handleLoad(tree, treeNode, resolve) {
      child(this.baseUrl, tree.id, this.queryParams).then((res) => {
        return resolve(res.data);
      });
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd(row) {
      this.$refs.model.add(row != null && row.id ? row.id : '0')
    },
    /** 修改按钮操作 */
    handleEdit(row) {
      this.$refs.model.edit(row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除名称为"' + row.name + '"的组织及其所有的下属组织？').then(() => {
        remove(this.baseUrl, row.id, true).then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        }).catch(() => {
        });
      });
    }
  }
};
</script>
